package com.adingxiong.quickstart.fliter;

/**
 * @ClassName TokenInterceptor
 * @Description TODO
 * @Author xiongchao
 * @Date 2020/5/21 17:26
 **/

import com.adingxiong.quickstart.result.Result;
import com.adingxiong.quickstart.result.RetCode;
import com.adingxiong.quickstart.util.TokenUtils;
import com.alibaba.fastjson.JSONObject;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 配置token拦截器
 */
public class TokenInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        /*//查看访问路径
        String pathUrl = request.getRequestURI();
        if(pathUrl.equals("/dev/doc.html") || pathUrl.equals("/dev/user/login")
                || pathUrl.contains("/dev/webjars/" )|| pathUrl.equals("/dev/v2/api-docs")
                || pathUrl.contains("/dev/swagger")
                ){
            return true;
        }
        //获取token
        String token = request.getHeader(TokenUtils.tokenHeader);
        if(token ==null){
            //response.sendRedirect("/pm/user/login");
            Result result = new Result(10020,"token失效,请重新登录",false);
            response.getWriter().write(JSONObject.toJSONString(result));
            return false;
        }
        try {
            TokenUtils.getTokenBody(token);

        } catch (Exception e){
            e.printStackTrace();
            //response.sendRedirect("/pm/user/login");
            response.getWriter().append("system error! please try  later");
            return false;
        }*/
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }

}
